package com.example.makerspace.dao;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.example.makerspace.domain.UsersOnlineDO;
import com.example.makerspace.dto.Pagination;
import com.example.makerspace.mapper.UsersOnlineMapper;
import org.apache.commons.lang.StringUtils;
import org.springframework.stereotype.Component;

import javax.annotation.PostConstruct;
import javax.annotation.Resource;
import java.util.List;

@Component
public class UsersOnlineDAO extends  BaseDAO<UsersOnlineDO> {
    @Resource
    private UsersOnlineMapper usersOnlineMapper;

    @PostConstruct
    public void init(){
        this.mapper = usersOnlineMapper;
    }

    public Page<UsersOnlineDO> getOnlineUser(Page<UsersOnlineDO> page, String username, String address) {
        LambdaQueryWrapper<UsersOnlineDO> qw = new LambdaQueryWrapper<UsersOnlineDO>();
                qw.and(w->w.like(StringUtils.isNotEmpty(username),UsersOnlineDO::getUsername,username)
                                .or()
                                .like(StringUtils.isNotEmpty(username),UsersOnlineDO::getNickname,username))
                        .like(StringUtils.isNotEmpty(address),UsersOnlineDO::getMapAddress,address);
               return  mapper.selectPage(page,qw);
    }

    public Page<UsersOnlineDO> selectByPage(Pagination pagination) {
        QueryWrapper<UsersOnlineDO> wrapper = new QueryWrapper<>();
        return  mapper.selectPage(new Page<UsersOnlineDO>(pagination.getPageNum(),pagination.getSize()),wrapper);
    }

    public Page<UsersOnlineDO> selectByPageAddress(Pagination pagination, String address) {
        QueryWrapper<UsersOnlineDO> wrapper = new QueryWrapper<>();
        wrapper.like("map_address",address);
        return  mapper.selectPage(new Page<UsersOnlineDO>(pagination.getPageNum(),pagination.getSize()),wrapper);
    }

    public List<UsersOnlineDO> selectByUserName(String username) {
        QueryWrapper<UsersOnlineDO> wrapper = new QueryWrapper<>();
        wrapper.eq("username",username);
        return mapper.selectList(wrapper);
    }
}
